package cmd

import (
	"github.com/spf13/cobra"
	global2 "longmen/server/config/global"
	"longmen/server/internal/services/consumers/config"
	"longmen/server/internal/services/consumers/consumer/chan_stats"
	_const "longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/common/lib/rabbitmq"
)

var chanStatsCli = &cobra.Command{
	Use:   _const.PusherChanStats,
	Short: "渠道统计 ",
	Long:  "",
	Run: func(cmd *cobra.Command, args []string) {
		mq := rabbitmq.New(&config.RabbitConf, global2.Log)
		exchange, queue, options, consumerOptions := rabbitmq.GetRabbitConsumerConfig(global2.Config, global2.AppName)
		// 开启consumer
		consumer, err := mq.NewConsumer(exchange, queue, options, consumerOptions)
		if err != nil {
			global2.Log.Fatalf("new consumer[%s]: err : %v", global2.AppName, err)
		}
		stats := chan_stats.NewChannelStatistic(global2.Log)
		err = consumer.Consume(stats.Process)
		if err != nil {
			global2.Log.Fatalf("run consumer[%s]: err : %v", global2.AppName, err)
		}
		gracefulShutdown()
	},
	PreRun: func(cmd *cobra.Command, args []string) {
		global2.AppName = _const.PusherChanStats
		global2.Initialize(config.ConfigPath)
		config.InitConsumerConfig()
		//pyroscope.InitPyroScope(global2.AppName)
	},
}
